import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**福利签到属性枚举*/
export const enum E_WelfareSignIn {
	/**已签到天数*/
	SIGN_DAY = 0,
	/**上次签到时间*/
	LAST_SIGN_TIME = 1,
	/**枚举数量*/
	EnumCount = 2
}


/**福利签到*/
export class WelfareSignIn {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_WELFARE_SIGN_IN_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_WELFARE_SIGN_IN_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: WelfareSignIn[] = [];

	/**创建入口*/
	public static create(json?: string): WelfareSignIn {
		const c = this.POOL.pop() || new WelfareSignIn();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**已签到天数 (uint16)*/
	protected _signDay: number;
	/**上次签到时间 (long)*/
	protected _lastSignTime: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (WelfareSignIn.POOL.indexOf(this) === -1) {
			WelfareSignIn.POOL.push(this);
		}
	}

	/**已签到天数*/
	public get signDay() {
		return this._signDay;
	}


	/**已签到天数*/
	public set signDay(v: number) {
		this._signDay = v;
	}


	/**上次签到时间*/
	public get lastSignTime() {
		return this._lastSignTime;
	}


	/**上次签到时间*/
	public set lastSignTime(v: number) {
		this._lastSignTime = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_WelfareSignIn): any {
		switch (k) {
			case E_WelfareSignIn.SIGN_DAY: return this._signDay;
			case E_WelfareSignIn.LAST_SIGN_TIME: return this._lastSignTime;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_WelfareSignIn, v: any): void {
		switch (k) {
			case E_WelfareSignIn.SIGN_DAY: this._signDay = v; break;
			case E_WelfareSignIn.LAST_SIGN_TIME: this._lastSignTime = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//已签到天数
		this._signDay = 0;
		//上次签到时间
		this._lastSignTime = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._signDay) {
			json.signDay = this._signDay;
		}
		if (this._lastSignTime) {
			json.lastSignTime = this._lastSignTime;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.signDay) {
			this._signDay = json.signDay;
		}
		if (json.lastSignTime) {
			this._lastSignTime = json.lastSignTime;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint16(this._signDay);
		byte.writeLongNumber(this._lastSignTime);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._signDay = byte.readUint16();
		this._lastSignTime = byte.readLongNumber();
		return this;
	}


	/**克隆*/
	public clone(): WelfareSignIn {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = WelfareSignIn.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = WelfareSignIn.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//已签到天数
			if (idx === E_WelfareSignIn.SIGN_DAY) {
				const oldv = this._signDay;
				this._signDay = us[i + 1];
				IAsync.dispatch(eventID, idx, this._signDay, oldv);
				i += 2;
				continue;
			}
			//上次签到时间
			if (idx === E_WelfareSignIn.LAST_SIGN_TIME) {
				const oldv = this._lastSignTime;
				this._lastSignTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastSignTime, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in WelfareSignIn with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: WelfareSignIn): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._signDay !== other._signDay) {
			updates.push(E_WelfareSignIn.SIGN_DAY, this._signDay);
		}
		if (this._lastSignTime !== other._lastSignTime) {
			updates.push(E_WelfareSignIn.LAST_SIGN_TIME, this._lastSignTime);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(WelfareSignIn.TOTAL_EVENT_ID, this.getV(0));
	}

}
